package com.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.entity.AppCategory;
import com.app.entity.AppInfo;
import com.app.entity.AppVersion;
import com.app.entity.DataDictionary;
import com.app.entity.bean.AppInfoCondition;
import com.app.entity.bean.Page;
import com.app.service.AppInfoService;
import com.app.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 后台管理对应的控制器
 */
@Controller
@RequestMapping("/backend")
public class BackendController {
    @Autowired
    private AppInfoService appService;
    @Autowired
    private CommonService commonService;

    @RequestMapping("/main")
    public String index() {
        return "backend/main";
    }

    @RequestMapping("/applist")
    public String appList(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                          @RequestParam(value = "querySoftwareName", required = false) String softwareName,
                          @RequestParam(value = "queryFlatformId", required = false) Long flatformId,
                          @RequestParam(value = "queryCategoryLevel1", required = false) Long categoryLevel1,
                          @RequestParam(value = "queryCategoryLevel2", required = false) Long categoryLevel2,
                          @RequestParam(value = "queryCategoryLevel3", required = false) Long categoryLevel3,
                          Model model) {
        //封装查询条件
        AppInfoCondition condition = new AppInfoCondition(softwareName, flatformId,
                categoryLevel1, categoryLevel2, categoryLevel3, pageIndex, 5);
        //根据查询条件查询分页的app信息
        Page<AppInfo> appPage = appService.getAppPage(condition);
        //查询所有平台信息
        List<AppCategory> oneCategoryList = commonService.getOneCategoryList();
        List<DataDictionary> flatformList = commonService.getFlatformList();
        model.addAttribute("appPage", appPage);
        model.addAttribute("oneCategoryList", oneCategoryList);
        model.addAttribute("flatformList", flatformList);

        //回显查询条件
        if (softwareName != null && softwareName.length() != 0) {
            model.addAttribute("querySoftwareName", softwareName);
        }
        if (flatformId != null && flatformId != 0) {
            model.addAttribute("queryFlatformId", flatformId);
        }
        //如果查询条件中有一级分类，则同时还要查询其下的所有二级分类
        if (categoryLevel1 != null && categoryLevel1 != 0) {
            model.addAttribute("queryCategoryLevel1", categoryLevel1);
            //查询所有二级分类
            List<AppCategory> twoCategoryList = commonService.getCategoryListByParentId(categoryLevel1);
            model.addAttribute("twoCategoryList", twoCategoryList);
        }
        //如果查询条件中有二级分类，则同时还要查询其下的所有三级分类
        if (categoryLevel2 != null && categoryLevel2 != 0) {
            model.addAttribute("queryCategoryLevel2", categoryLevel2);
            //查询所有三级分类
            List<AppCategory> threeCategoryList = commonService.getCategoryListByParentId(categoryLevel2);
            model.addAttribute("threeCategoryList", threeCategoryList);
        }
        if (categoryLevel3 != null && categoryLevel3 != 0) {
            model.addAttribute("queryCategoryLevel3", categoryLevel3);
        }

        return "backend/applist";
    }

    @RequestMapping(value = "/clist", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String categoryList(@RequestParam(value = "pid", required = false) Long parentId) {
        //根据父id查询app分类
        List<AppCategory> categoryList = commonService.getCategoryListByParentId(parentId);
        String json = JSONObject.toJSONString(categoryList);
        return json;
    }

    @RequestMapping("/appcheck")
    public String appCheck(@RequestParam("aid") Long appId,
                           @RequestParam("vid") Long versionId,
                           Model model) {
        //查询app信息
        AppInfo appInfoById = appService.getAppInfoById(appId);
        //查询版本信息
        AppVersion appVersionById = appService.getAppVersionById(versionId);
        model.addAttribute("appInfoById", appInfoById);

        model.addAttribute("appVersionById", appVersionById);
        return "backend/appcheck";
    }
    @RequestMapping("/checksave")
    public String checkSave(@RequestParam("id") Long appId,
                            @RequestParam("status") Long status,
                            Model model) {
        //修改app状态
        boolean b = appService.updateAppInfo(appId, status);
        if (!b) {
            model.addAttribute("msg", "审核失败");
            return "backend/appcheck";
        }
        model.addAttribute("msg", "审核成功");
        return "redirect:/backend/applist";
    }
}
